﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 页面块类。
	/// </summary>
	public class PageBlock : System.Web.UI.UserControl
	{
		private LoadedValue<int?> loadedSiteId;
		/// <summary>
		/// 页面块所属的站点。
		/// </summary>
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () =>
					{
						int? siteId = Sites.CurrentSiteId;
						if (siteId == null) siteId = (int?)ViewState["SiteId"];
						return siteId;
					};
				return loadedSiteId.Value;
			}
			set
			{
				if (loadedSiteId.Setter == null)
					loadedSiteId.Setter = v => ViewState["SiteId"] = v;
				loadedSiteId.Value = value;
			}
		}

		private LoadedValue<string> loadedCode;
		/// <summary>
		/// 页面块的代码。
		/// </summary>
		public virtual string Code
		{
			get
			{
				if (loadedCode.Loader == null)
					loadedCode.Loader = () => (string)ViewState["Code"];
				return loadedCode.Value;
			}
			set
			{
				if (loadedCode.Setter == null)
					loadedCode.Setter = v => ViewState["Code"] = v;
				loadedCode.Value = value;
			}
		}

		private LoadedValue<Module.PageBlock> loadedBlock;
		/// <summary>
		/// 页面块的内容。
		/// </summary>
		public Module.PageBlock Block
		{
			get
			{
				if (loadedBlock.Loader == null)
					loadedBlock.Loader = () => (SiteId.HasValue && !string.IsNullOrEmpty(Code)) ? PageBlocks.GetItem(SiteId.Value, Code) : new Module.PageBlock();
				return loadedBlock.Value;
			}
		}
	}
}